<template>

  <div class="app-container">

    <el-form ref="form" :model="form" label-width="80px">

      <el-form-item label="标题">
        <el-input v-model="form.title"></el-input>
      </el-form-item>

      <el-form-item label="分类">
        <el-select v-model="form.classify_id" placeholder="请选择">
          <el-option
            v-for="item in classifies"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="关键字">
        <el-input v-model="form.key_word"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="form.description"></el-input>
      </el-form-item>

      <el-form-item label="封面">
        <el-upload
          class="avatar-uploader"
          action="/guard/upload?module=article"
          accept=".png,.jpg,.jpeg"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="上架/下架">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="form.up_down"
          active-text="上架"
          inactive-text="下架">
        </el-switch>
      </el-form-item>

      <el-form-item label="是否置顶">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="form.is_top"
          active-text="是"
          inactive-text="否">
        </el-switch>
      </el-form-item>

      <el-form-item label="正文">
        <div id="div1">
        </div>
      </el-form-item>

      <el-form-item label="">
        <el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
        <!-- <el-button @click="closeAddDialog" >重置</el-button> -->
      </el-form-item>



    </el-form>

  </div>


</template>

<script>
import { myGet, myPost } from '@/utils/myRequest'
import E from 'wangeditor'
const urlArticleClassifyLst = '/guard/article/classify/lst'
const urlArticleAdd = '/guard/article/add'
export default {

  data() {
    return {
      classifies:[],
      imageUrl:'',
      editor:null,
      form:{
        title:'',
        classify_id:'',
        key_word:'',
        description:'',
        cover_url:'',
        up_down:1,
        is_top:1,
        content:''
      },
    };
  },
  methods:{
    onSubmit(formName) {
      this.$refs[formName].validate((valid)=>{
        if(valid){
          this.form.content = this.editor.txt.html()
          myPost(urlArticleAdd,this.form).then(res => {
            if (res.status) {
              this.$message.success(res.msg)
              this.$router.push('/article/lst')
            }
          })
        }
      })
    },
    handleAvatarSuccess(res, file) {
      if(res.status){
        this.imageUrl = res.data.records.url
        this.form.cover_url = res.data.records.filePath
      }else{
        this.$message.error('上传失败!')
      }
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    closeAddDialog(){
      Object.assign(this.form ,this.$options.data().form)
    },
    initEditor(){
      this.editor = new E('#div1')
      this.editor.config.uploadImgServer = '/guard/article/uploadImg'
      this.editor.config.uploadVideoServer = '/guard/article/uploadVideo'
      this.editor.create()
    }
  },


  mounted() {
    this.initEditor()
    myGet(urlArticleClassifyLst).then(res=>{
      if(res.status){
        res.data.records.forEach(item=>{
          this.classifies.push({value: item.id, label: item.name})
        })
      }
    })
  }
}
</script>

<style >

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
